<?php

namespace App\Controller;

use Redis;


/**
 * 测试redis
 */
class ControllerRedis
{

    /**
     * 测试1
     */
    public function actionIndex()
    {
        //连接本地的 Redis 服务
        $redis = new Redis();
        $redis->connect('192.168.56.103', 6379);
        echo "Connection to server sucessfully";
        //string类型
        $redis->set("tutorial-name", "Redis tutorial");
        // 获取存储的数据并输出
        echo "Stored string in redis:: " . $redis->get("tutorial-name");

        //$redis->set("total", 11);
        $total = $redis->incr("total");
        var_dump($total);
    }

    public function actionList()
    {
        $redis = new Redis();
        $redis->connect('192.168.56.103', 6379);

        $redis->lPush('list1', 11);
        $redis->lPush('list1', 22);
        var_dump($redis->rPop('list1'));
        var_dump($redis->lLen('list1'));
    }

    public function actionSet()
    {
        $redis = new Redis();
        $redis->connect('192.168.56.103', 6379);

        $redis->sAdd("set1", "哈哈哈1");
        $redis->sAdd("set1", "哈哈哈2");
        var_dump($redis->scard("set1"));
        var_dump($redis->sismember("set1", "哈哈哈"));
        $redis->srem("set1", "哈哈哈");
        var_dump($redis->sismember("set1", "哈哈哈"));
    }

    public function actionHash()
    {
        $redis = new Redis();
        $redis->connect('192.168.56.103', 6379);

        $redis->hSet("hash1", "key1", "val1");
        $redis->hSet("hash1", "key2", "val2");
        $redis->hSet("hash1", "key3", "val3");
        var_dump($redis->hGet("hash1", "key1"));
        var_dump($redis->hLen("hash1"));
        var_dump($redis->hKeys("hash1"));
        var_dump($redis->hMget("hash1", ["key1", "key3"]));
    }

    public function actionSortSet()
    {
        $redis = new Redis();
        $redis->connect('192.168.56.103', 6379);

        $redis->zAdd("zset1", 1.1, "haha1.11");
        $redis->zAdd("zset1", 1.1, "haha1.1");
        $redis->zAdd("zset1", 2.2, "haha2.2");

        $redis->zAdd("zset1", 3.3, "haha0.0");
        $redis->zAdd("zset1", 0.0, "haha0.0");

        var_dump($redis->zCard("zset1"));

        var_dump($redis->zRange("zset1", 0, -1));
        var_dump($redis->zRange("zset1", 0, -1, true));
        var_dump($redis->zRangeByScore("zset1", 0, 3));
    }

    /**
     * 模拟自选股行情
     */
    public function actionStockList1()
    {
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(0);
        $symbols = ['NVDA', 'BABA', 'BBD', 'BBU', 'BBL'];
        $rows = [];
        foreach ($symbols as $symbol) {
            $rows[$symbol] = $redis->hGetAll($symbol);
        }
        var_dump($rows);
    }

    /**
     * 模拟升序排序
     */
    public function actionStockList2()
    {
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(1);

        $page = 1;
        $size = 10;
        $index_bgn = ($page - 1) * $size;
        $index_end = $page * $size - 1;

        $rows = $redis->zRange('current', $index_bgn, $index_end, true);
        var_dump($rows);
    }

    /**
     * 模拟降序排序
     */
    public function actionStockList3()
    {
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(1);

        $page = 1;
        $size = 10;
        $index_bgn = ($page - 1) * $size;
        $index_end = $page * $size - 1;

        //先取得排序列表
        $symbols = $redis->zRevRange('current', $index_bgn, $index_end);

        //补齐详情数据
        $redis->select(0);
        $rows = [];
        foreach ($symbols as $symbol) {
            $rows[$symbol] = $redis->hGetAll($symbol);
        }
        var_dump($rows);
    }
}
